<?php 
	defined('IN_PHPCMS') or exit('NO permission resoures.');
	class index {
		function __construct(){	
			$this->username=param::get_cookie('_username');
			if($this->username!=null){
				$this->address_db=pc_base::load_model('address_model');				
			}else{
				exit('here');
				showmessage('请登录后尝试此操作','index.php');
			}
			
			
		}
		
		function init(){
			if(isset($_POST['dosubmit'])){				
			   $add_address=Array();
			   $add_address['username']=$this->username;
			   $add_address['consignee']=isset($_POST['consignee'])?safe_replace($_POST['consignee']):exit('0');
			   $add_address['mobile']=isset($_POST['mobile'])?safe_replace($_POST['mobile']):exit('0');
			   $add_address['tel']=isset($_POST['tel'])?safe_replace($_POST['tel']):exit('0');
			   $add_address['city']=isset($_POST['city'])?safe_replace($_POST['city']):"客户未填写";
			   $add_address['postcode']=isset($_POST['postcode'])?safe_replace($_POST['postcode']):exit('0');
			   $add_address['identitycard']=isset($_POST['identitycard'])?safe_replace($_POST['identitycard']):exit('0');
			   $add_address['e_address']=isset($_POST['e_address'])?safe_replace($_POST['e_address']):"客户未填写";
			   $add_address['c_address']=isset($_POST['c_address'])?safe_replace($_POST['c_address']):exit('0');
			  // $add_address['state']=isset($_POST['state'])?safe_replace($_POST['state']):exit('0');
			   $add_address['company']=isset($_POST['company'])?safe_replace($_POST['company']):exit('0');
			   if($_GET['id']){
			   	//如果是更改用户收货地址
			   		$id=$_GET['id'];
			   		//echo "已经入修改环节";
			   		$this->address_db->update($add_address,"id=$id");
			   		showmessage('修改成功','index.php?m=address&c=index&a=show_address');
			   }else{
			   	//重新插入
			   		$this->address_db->insert($add_address);
			   		showmessage('插入数据成功','index.php?m=address&c=index&a=show_address');
			   }
			}else{
				showmessage('请通过正常途径提交表单','index.php?m=address&a=index&c=show_address');
			}
		}
		function add_address(){
			include template('address','add_address');
			
			
		}
		function show_address(){
			$where = "username='$this->username'";
			$order = 'id desc';
			$pagesize =2; 
			$limit=10;
			$data=$this->address_db->select($where, $data = '*', $limit, $order, $group = '', $key='');
			
			include template('address','show');		
			return $data;
			
		}
		function change(){
			//print_r($_POST);
			if(isset($_POST['id'])){
				if($_POST['submit']){
					$id=isset($_POST['id'])?$_POST['id']:exit('系统错误，请刷新');
					$this->address_db->update("state=2","state=1 and username='$this->username'");
					$this->address_db->update("state=1","id=$id");
					header("Location:index.php?m=address&c=index&a=show_address");
					//					
				}elseif ($_POST['dosubmit']){
					$id=isset($_POST['id'])?$_POST['id']:exit('系统错误，请刷新');
					header("Location:index.php?m=address&c=index&a=modify&id=$id&modify=true");
				}elseif ($_POST['delsubmit']){
					$id=isset($_POST['id'])?$_POST['id']:exit('系统错误，请刷新');
					$a_state=$this->address_db->get_one("id=$id");
					if($a_state['state']==1){
						echo "<script>alert('默认收货地址不能被删除，请先修改其状态');window.history.go(-1);</script>";
					}else{
						$query=$this->address_db->delete("id=$id limit 1");
						if($query){
							showmessage('删除成功','index.php?m=address&c=index&a=show_address');
						}
					}
					
					
				}	
			}else{
				echo "<script>alert('请勾选需要操作的地址');window.history.go(-1);</script>";
			}				
			
		}
		function modify(){
			if($_GET['modify']==true){
				$id=isset($_GET['id'])?$_GET['id']:exit('更改地址号错误');
				//echo $id;
				$data=$this->address_db->get_one("id='$id'");
				include template('address','modify');
			}else{
				echo "<script>window.history.go(-1);</script>";
			}
		}
		
	}





?>